﻿
Imports xmlctrl
Imports elpmis
Imports lpmsengi

Public Class register
    Inherits requestProcessor

    Public Const _pathidentity As String = network.pathSeperator + "reg"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Function processRegister(ByVal context As httpContext, _
                                            ByVal id As String, _
                                            ByVal existing As Boolean, _
                                            ByVal invalid As Boolean) As Boolean
        assert(Not (existing AndAlso invalid))
        elpmis.context.insertThreadStaticVariable("id", typeDefination.string, id)
        elpmis.context.insertThreadStaticVariable("existing", typeDefination.bool, existing)
        elpmis.context.insertThreadStaticVariable("invalid", typeDefination.bool, invalid)
        Return context.createControl(New nameLpmsControl("register")).render()
    End Function

    Private Shared Function processRegisterSuccess(ByVal context As httpContext, _
                                                   ByVal id As String) As Boolean
        elpmis.context.insertThreadStaticVariable("id", typeDefination.string, id)
        Return context.createControl(New nameLpmsControl("register_success")).render()
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        If antiInputSpam.checkIpTable(context) Then
            Dim id As String = Nothing
            Dim pwd As String = Nothing
            id = context.url().query(query.id)
            pwd = context.url().query(query.password)
            If Not isemptystring(id) AndAlso Not isemptystring(pwd) Then
                If idVarify(id) Then
                    If setUserPassword(id, pwd) Then
                        If Not setUserRandomKey(context, id, True) Then
                            raiseError(errorHandle.errorType.warning, "cannot setUserRandomKey, id ", id)
                        End If
                        Return processRegisterSuccess(context, id)
                    Else
                        Return processRegister(context, id, True, False)
                    End If
                Else
                    Return processRegister(context, id, False, True)
                End If
            Else
                Return processRegister(context, id, existUser(id), False)
            End If
        Else
            Return processInputSpamming(context)
        End If
    End Function
End Class
